package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.GetRefundBillBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.entities.RefundBill;
import com.ymatou.iapi.trading.sellerquery2.parameter.entities.RefundBillProduct;
import com.ymatou.iapi.trading.sellerquery2.parameter.resp.RefundBillResp;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * 退款单号获取退款信息接口 FreeMaker Template自动生成代码
 * 接口已经转接java
 */
@TestSuite
public class Ts_GetRefundBill {
	private static GetRefundBillBean getrefundbillBean;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetRefundBillCall getrefundbillCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退款单号获取退款信息接口");
	}

	@Before
	public void caseUp() {
		getrefundbillBean = new GetRefundBillBean();
		getrefundbillCall = new com.ymatou.iapi.trading.sellerquery2.service.GetRefundBillCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getrefundbillCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		DataUtil.verify(200, getrefundbillCall.getCode(), "验证code");
		RefundBillResp expectResponse = new RefundBillResp();
		ConstructExpectedResponse(expectResponse);
		RefundBillResp actualResponse = getrefundbillCall.getRefundBillResp();
		RefundBill expect = expectResponse.getRefundBillandProduct();
		RefundBill actual = actualResponse.getRefundBillandProduct();
		if(expect == null){
			DataUtil.verify(null, actual, "验证RefundBillandProduct == null");
		}else{
			DataUtil.verify(expect.getOrderId(), actual.getOrderId(),"验证OrderId");
			DataUtil.verify(expect.getRrefundAmount(), actual.getRrefundAmount(),"验证RrefundAmount");
			DataUtil.verify(expect.getRefundBillNo(), actual.getRefundBillNo(),"验证RefundBillNo");
			DataUtil.verify(expect.getRefundedAmountOfCash(), actual.getRefundedAmountOfCash(),"验证RefundedAmountOfCash");
			DataUtil.verify(expect.getRefundedAmountOfGift(), actual.getRefundedAmountOfGift(),"验证RefundedAmountOfGift");
			List<RefundBillProduct> exps = expect.getRefundBillProductList();
			List<RefundBillProduct> acts = actual.getRefundBillProductList();
			if(DataUtil.isNullOrEmpty(exps)){
				DataUtil.verify(true, DataUtil.isNullOrEmpty(acts),"验证RefundBillProductList为空");
			}else{
				boolean sameCount = exps.size()==acts.size()?true:false;
				DataUtil.verify(exps.size(), acts.size(),"验证RefundBillProductList的数量");
				if(sameCount){
					for(int i = 0;i<exps.size();i++){
						RefundBillProduct exp = exps.get(i);
						RefundBillProduct act = acts.get(i);
						verifyRefundBillProduct(i, exp, act);
					}
				}
			}
		}
	}
	
	private void verifyRefundBillProduct(int i, RefundBillProduct expect,RefundBillProduct actual){
		i += 1;
		DataUtil.verify(expect.getAmount(), actual.getAmount(),"验证第"+i+"条RefundBillProduct的Amount");
		DataUtil.verify(expect.getDiscount(), actual.getDiscount(),"验证第"+i+"条RefundBillProduct的Discount");
		DataUtil.verify(expect.getFreight(), actual.getFreight(),"验证第"+i+"条RefundBillProduct的Freight");
		DataUtil.verify(expect.getOrderInfoId(), actual.getOrderInfoId(),"验证第"+i+"条RefundBillProduct的OrderInfoId");
		DataUtil.verify(expect.getProductPrice(), actual.getProductPrice(),"验证第"+i+"条RefundBillProduct的ProductPrice");
		DataUtil.verify(expect.getSellerCouponAmount(), actual.getSellerCouponAmount(),"验证第"+i+"条RefundBillProduct的SellerCouponAmount");
		DataUtil.verify(expect.getYmtCouponAmount(), actual.getYmtCouponAmount(),"验证第"+i+"条RefundBillProduct的YmtCouponAmount");
	}

	@SuppressWarnings("rawtypes")
	private void ConstructExpectedResponse(RefundBillResp response) {
		List<Map> results = execute(getrefundbillBean.getRefundBillNo(), 0);
		if (DataUtil.isNullOrEmpty(results)) {
			response.setRefundBillandProduct(null);
		} else {
			RefundBill o = new RefundBill();
			List<RefundBillProduct> RefundBillProductList = new ArrayList<RefundBillProduct>();
			DataUtil.mapToBean(o, results.get(0));
			for (Map m : results) {
				RefundBillProduct prod = new RefundBillProduct();
				DataUtil.mapToBean(prod, m);
				RefundBillProductList.add(prod);
			}
			o.setRefundBillProductList(RefundBillProductList);
			response.setRefundBillandProduct(o);
		}
	}

	@SuppressWarnings("rawtypes")
	public static List<Map> execute(String RefundBillNo, Integer OrderId) {
		String sql = GetRefundBill(RefundBillNo,
				DataUtil.Integerr.GetValueOrDefault(OrderId));
		return new DefaultWapper().executeSql(sql);
	}

	public static String GetRefundBill(String RefundBillNo, int OrderId) {
		String s = "SELECT"
				+ " a.refundBillNo,"
				+ " a.orderId,"
				+ " a.refundAmount,"
				+ " a.refundedAmountOfCash,"
				+ " a.refundedAmountOfGift,"
				+ " a.settlementAmountOfCoupon as 'refundedAmountOfCoupon',"
				+ " a.refundedDiscountOfThirdParty as 'refundedDiscountOfThirdParty',"
				+ " a.salesRefundStatus," + " b.orderInfoId,"
				+ " c.fProductPrice AS 'productPrice',"
				+ " c.iAmount AS 'amount'," + " c.fFreight AS 'freight',"
				+ " c.fDiscount AS 'discount',"
				+ " c.fYmtCouponAmount AS 'ymtCouponAmount',"
				+ " c.fSellerCouponAmount AS 'sellerCouponAmount'"
				+ " FROM dbo.Ymt_RefundBill a ( NOLOCK )"
				+ " JOIN dbo.Ymt_RefundProduct b ( NOLOCK )"
				+ " ON a.RefundBillNo = b.RefundBillNo"
				+ " JOIN dbo.Ymt_OrderInfo c ( NOLOCK )"
				+ " ON b.OrderInfoId = c.sOrderInfoId";
		if (OrderId > 0) {
			return s + " WHERE a.OrderId = " + OrderId;
		} else {
			return s + " WHERE a.RefundBillNo = '" + RefundBillNo + "'";
		}
	}
	
	private String getRefundNo(int orderId){
		return (String)new DefaultWapper().executeSql("select top 1 RefundBillNo from ymt_refundbill where orderid = "+orderId).get(0).get("RefundBillNo");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetRefundBill_001() {
		Logger.start(true, "退款单号获取退款信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getrefundbillBean.setOrderId(orderId);
			getrefundbillBean.setRefundBillNo(getRefundNo(orderId));
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetRefundBill_002() {
		Logger.start(true, "退款单号不存在");
		try {

			getrefundbillBean.setRefundBillNo("404");
			getrefundbillBean.setOrderId(112482251);
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetRefundBill_003() {
		Logger.start(true, "退款单号为null");
		try {
			getrefundbillBean.setRefundBillNo(null);
			getrefundbillBean.setOrderId(null);
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			DataUtil.verify(400, getrefundbillCall.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	@Ignore
	public void Tc_GetRefundBill_004() {
		Logger.start(true, "订单号不存在");
		try {
			getrefundbillBean.setRefundBillNo("TH1512110004");
			getrefundbillBean.setOrderId(404);
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetRefundBill_005() {
		Logger.start(true, "sellerquery2冒烟测试");
		try {
			int orderId = 112483272;
			getrefundbillBean.setOrderId(orderId);
			getrefundbillBean.setRefundBillNo("TH0"+orderId);
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetRefundBill_006() {
		Logger.start(true, "退款单号获取退款失败的信息成功");
		try {
			Logger.comment("准备数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundFailedOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备数据");
			getrefundbillBean.setOrderId(orderId);
			getrefundbillBean.setRefundBillNo(getRefundNo(orderId));
			String param = FormatUtil.beanToHttpString(getrefundbillBean, true,
					false);
			getrefundbillCall.setData(param);
			getrefundbillCall.callService();
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}